package com.wty.cmims.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wty.cmims.pojo.Code;
import com.wty.cmims.pojo.Evaluate;
import com.wty.cmims.pojo.ResponseResult;
import com.wty.cmims.service.EvaluateService;
import com.wty.cmims.mapper.EvaluateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author wty
* @description 针对表【evaluate】的数据库操作Service实现
* @createDate 2025-03-24 16:25:12
*/
@Service
public class EvaluateServiceImpl extends ServiceImpl<EvaluateMapper, Evaluate>
    implements EvaluateService{
    @Autowired
    private EvaluateMapper evaluateMapper;
    @Override
    public ResponseResult<Map<String, Object>> evaluateAdd(Map<String, Object> paramsMap) {
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        if (paramsMap.get("appointmentId") != null && paramsMap.get("score") != null && paramsMap.get("content") != null) {
            Evaluate evaluate = new Evaluate();
            evaluate.setAppointmentId(Integer.parseInt(paramsMap.get("appointmentId").toString()));
            evaluate.setScore(Integer.parseInt(paramsMap.get("score").toString()));
            evaluate.setContent(paramsMap.get("content").toString());
            // 如果已有记录,则修改
            LambdaQueryWrapper<Evaluate> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Evaluate::getAppointmentId, evaluate.getAppointmentId());
            queryWrapper.eq(Evaluate::getIsDel, 0);
            Evaluate evaluateExist = this.getOne(queryWrapper);

            if (evaluateExist != null) {
                evaluateExist.setScore(evaluate.getScore());
                evaluateExist.setContent(evaluate.getContent());
                // 更新时间戳
                Date date = new Date();
                evaluateExist.setTime(date);
                this.updateById(evaluateExist);
                responseResult.setCode(Code.SUCCESS);
                responseResult.setMessage("评价修改成功");
                return responseResult;
            }
            // 如果没有记录，则新增
            if (this.save(evaluate)) {
                responseResult.setCode(Code.SUCCESS);
                responseResult.setMessage("评价成功");
            } else {
                responseResult.setCode(Code.ERROR);
                responseResult.setMessage("评价失败");
            }
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Evaluate> evaluateFind(Map<String, Object> paramsMap) {
        ResponseResult<Evaluate> responseResult = new ResponseResult<>();
        if (paramsMap.get("appointmentId") != null) {
            LambdaQueryWrapper<Evaluate> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Evaluate::getAppointmentId, paramsMap.get("appointmentId"));
            queryWrapper.eq(Evaluate::getIsDel, 0);
            Evaluate evaluate = this.getOne(queryWrapper);
            if (evaluate != null) {
                responseResult.setCode(Code.SUCCESS);
                responseResult.setMessage("查询成功");
                responseResult.setData(evaluate);
            }
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Map<String, Object>> evaluateDelete(Map<String, Object> paramsMap) {
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        if (paramsMap.get("appointmentId") != null) {
            LambdaQueryWrapper<Evaluate> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Evaluate::getAppointmentId, paramsMap.get("appointmentId"));
            queryWrapper.eq(Evaluate::getIsDel, 0);
            Evaluate evaluate = this.getOne(queryWrapper);
            if (evaluate != null) {
                evaluate.setIsDel(1);
                if (this.updateById(evaluate)) {
                    responseResult.setCode(Code.SUCCESS);
                    responseResult.setMessage("删除成功");
                } else {
                    responseResult.setCode(Code.ERROR);
                    responseResult.setMessage("删除失败");
                }
            }
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Map<String, Object>> evaluationsFind(Map<String, Object> paramsMap) {
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        Integer pageSize = (Integer) paramsMap.get("pageSize");
        paramsMap.put("start", ((Integer) paramsMap.get("currentPage") - 1) * pageSize);
        responseResult.setPageSize(pageSize);
        List<Map<String, Object>> evaluations = evaluateMapper.findEvaluations(paramsMap);
        Integer total = evaluateMapper.findEvaluationsNum(paramsMap);
        if (evaluations != null) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("患者评价查询成功");
            responseResult.setDatas(evaluations);
            responseResult.setTotal(total);
            responseResult.setCurrentPage((Integer) paramsMap.get("currentPage"));
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("患者评价查询失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Map<String, Object>> evaluationCompute(Map<String, Object> paramsMap) {
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        List<Integer> doctorIds = (List<Integer>) paramsMap.get("doctorIds");
        // 将医生ID和评分查询为一个List<Map<String, Object>>
        List<Map<String, Object>> list = new ArrayList<>();
        for(Integer doctorId : doctorIds){
            Map<String, Object> map = new HashMap<>();
            map.put("doctorId", doctorId);
            map.put("score", evaluateMapper.computeScore(map));
            map.putIfAbsent("score", 0);
            list.add(map);
        }
        if (!list.isEmpty()) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("评分计算成功");
            responseResult.setDatas(list);
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("评分计算失败");
        }
        return responseResult;
    }
}




